<?php

/**
 * Class chuyen xu ly user
 * @author anhmantk
 */
class User_Manager {

    /**
     * lay toan bo user trong he thong
     * @param string $name
     * @return array toan bo user 
     * @author anhmantk
     */
    public function getAllUsers($name = null) {
        $where = "";
        if($name != null) {
            $where = " AND u.name LIKE '%" . $name . "%'";
        }
        $query = "SELECT u.uid, u.name, u.status, ud.first_name, ud.last_name, ud.avatar, ud.address, (SELECT COUNT(uf.id) FROM {user_friends} AS uf WHERE uf.uid = u.uid) AS totalFriend FROM {users} AS u LEFT JOIN {users_data} AS ud ON (ud.uid = u.uid) WHERE u.uid NOT IN (0,1) " . $where;
        $count_query = "SELECT COUNT(u.uid) FROM {users} AS u WHERE 1 = 1 " . $where;

        $result = pager_query($query, USER_LIMIT_PERPAGE, 0, $count_query);
        while(($obj = db_fetch_object($result)) != null) {
            $rs[] = $obj;
        }
        $pager = theme('pager');
        return array($rs, $pager);
    }

    /**
     * lay lai thong tin co ban cua thanh vien
     * @param int $uid
     * @return stdClass thong tin thanh vien
     * @author anhmantk
     */
    public function getUserById($uid) {
        $query = "SELECT * FROM {users} WHERE uid = %d";
        return db_fetch_object(db_query($query, $uid));
    }

    /**
     *
     * @param array $params
     *        array(name, pass, mail, init, status)     
     * @return int last insert id
     * @author anhmantk
     */
    public function _addUser($params = array()) {
        $query = "INSERT INTO {users} (name, pass, mail, init, status) VALUES ('%s', '%s', '%s', '%s', %d)";
        if (db_query($query, $params)) {
            return db_last_insert_id('users', 'uid');
        } else {
            return -1;
        }
    }
    
    /**
     * sua thong tin nguoi dung
     * @param int $uid
     * @param int $status
     * @param string $pass MD5
     * @return boolean
     * @author anhmantk
     */
    public function _editUser($uid ,$status = 0, $pass = null){
        $params = array($status);
        $query = "UPDATE {users} SET status = %d ";
        if ($pass != null) {
            $query .= ",pass = '%s' ";
            $params[] = $pass;
        }
        $query .= " WHERE uid = %d";
        $params[] = $uid;
        return db_query($query, $params);
    }


    /**
     * khoa hoac bo khoa nguoi dung
     * @param int $uid
     * @param int $status
     * @return boolean
     * @author anhmantk
     */
    public function _lockUnLockUser($uid, $status) {
        $query = "UPDATE {users} SET status = %d WHERE uid = %d";
        return db_query($query, array($status, $uid));
    }
    
    /**
     * kiem tra user da ton tai hay chua
     * Neu da ton tai roi thi khong cho dang ky
     * va nguoc lai
     * @param string $name
     * @return boolean
     * @author anhmantk
     */
    public function userIsExits($name) {
        $query = "SELECT uid FROM {users} WHERE name = '%s'";
        if(db_fetch_array(db_query($query, $name)) != null) {
            return true;
        } 
        return false;
    }
    
    /**
     * kiem tra user da ton tai hay chua
     * Neu da ton tai roi thi khong cho dang ky
     * va nguoc lai
     * @param string $name
     * @return boolean
     * @author anhmantk
     */
    public function emailIsExits($email) {
        $query = "SELECT uid FROM {users} WHERE mail = '%s'";
        if(db_fetch_array(db_query($query, $email)) != null) {
            return true;
        } 
        return false;
    }
    
}

/**
 * Class chuyen xu ly phan user data
 */
class User_Data {

    /**
     * lay thong tin chi tiet cua user
     * @param int $uid
     * @return stdClass thong tin chi tiet cua user 
     * @author anhmantk
     */
    public function getUserDataById($uid) {
        $query = "SELECT * FROM {users_data} WHERE uid = %d";
        return db_fetch_object(db_query($query, $uid));
    }

    /**
     * them thong tin chi tiet cho thanh vien
     * @param aray $params
     * array(first_name, last_name, gender, birthday, address, telefone, fax, mobile,description, company, location_index,location, website, avatar, uid)
     * @return boolean
     */
    public function insertUserData($params = array()) {
        $query = "INSERT INTO {users_data} (first_name, last_name, gender, birthday, address, telefone, fax, mobile,description, company, location_index,location, website, avatar, uid) VALUES ('%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)";
        return db_query($query, $params);
    }

    /**
     * sua thong tin chi tiet cho thanh vien
     * @param array $params
     * array(first_name, last_name, gender, birthday, address, telefone, fax, mobile,description, company, location_index,location, website, avatar, uid)
     * @return boolean
     */
    public function updateUserData($params = array()) {
        $query = "UPDATE {users_data} SET first_name = '%s', last_name = '%s', gender = '%s', birthday = %d, address = '%s', telefone = '%s', fax = '%s', mobile = '%s',description = '%s', company = '%s', location_index = '%s',location = '%s', website = '%s', avatar = '%s' WHERE uid = %d";
        return db_query($query, $params);
    }

}

class Location {

    /**
     * lay toan bo danh sach tinh thanh
     * @return array stdClass
     */
    public function getAllLocation() {
        $query = "SELECT name, indexs FROM {location} ORDER BY name";
        $result = db_query($query);
        while(($obj = db_fetch_object($result)) != null) {
            $rs[] = $obj;
        }
        return $rs;
    }

}